package io.aircraft.modules.wms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.aircraft.common.page.PageData;
import io.aircraft.common.service.impl.CrudServiceImpl;
import io.aircraft.common.utils.ConvertUtils;
import io.aircraft.modules.sys.dto.SysUserDTO;
import io.aircraft.modules.sys.service.SysUserService;
import io.aircraft.modules.wms.dao.PurchaseDetailDao;
import io.aircraft.modules.wms.dto.PurchaseDetailDTO;
import io.aircraft.modules.wms.entity.PurchaseDetailEntity;
import io.aircraft.modules.wms.service.PurchaseDetailService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 入库明细
 *
 * @author Cycle gengruyi01@163.com
 * @since 1.0.0 2022-10-17
 */
@Service
public class PurchaseDetailServiceImpl extends CrudServiceImpl<PurchaseDetailDao, PurchaseDetailEntity, PurchaseDetailDTO> implements PurchaseDetailService {
    @Autowired
    private SysUserService sysUserService;

    @Override
    public PageData<PurchaseDetailDTO> page(Map<String, Object> params) {
        IPage<PurchaseDetailEntity> page = baseDao.selectPage(
                getPage(params, "detail_id", true),
                getWrapper(params)
        );
        //获取[sys_user]的信息
        List<SysUserDTO> userList = sysUserService.getList();
        //将用户编号填充至creatorName&updaterName
        page.getRecords().forEach(entity -> {
            entity.setCreatorName(sysUserService.getUsernameById(entity.getCreator(), userList));
            entity.setUpdaterName(sysUserService.getUsernameById(entity.getUpdater(), userList));
        });
        return getPageData(page, PurchaseDetailDTO.class);
    }

    @Override
    public QueryWrapper<PurchaseDetailEntity> getWrapper(Map<String, Object> params) {
        String purchaseId = (String) params.get("purchaseId");
        String contNo = (String) params.get("contNo");
        String spuNo = (String) params.get("spuNo");
        String lotNo = (String) params.get("lotNo");
        String detailSt = (String) params.get("detailSt");
        String startDate = (String) params.get("startDate");
        String endDate = (String) params.get("endDate");

        QueryWrapper<PurchaseDetailEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(purchaseId), "purchase_id", purchaseId);
        wrapper.eq(StringUtils.isNotBlank(detailSt), "detail_st", detailSt);
        wrapper.eq(StringUtils.isNotBlank(contNo), "cont_no", contNo);
        wrapper.eq(StringUtils.isNotBlank(spuNo), "spu_no", spuNo);
        wrapper.eq(StringUtils.isNotBlank(lotNo), "lot_no", lotNo);
        wrapper.ge(StringUtils.isNotBlank(startDate), "create_date", startDate);
        wrapper.le(StringUtils.isNotBlank(endDate), "create_date", endDate);
        return wrapper;
    }

    /**
     * 通过订单主键ID获取其对应的所有明细信息
     *
     * @param purchaseId 主单id
     * @param all        明细表中所有信息
     * @return
     */
    @Override
    public List<PurchaseDetailDTO> getDetailByPurchaseId(Long purchaseId, List<PurchaseDetailDTO> all) {
        return all.stream().filter(dto -> purchaseId.equals(dto.getPurchaseId())).collect(Collectors.toList());
    }

    @Override
    public List<PurchaseDetailDTO> getAllDetail() {
        List<PurchaseDetailEntity> entityList = baseDao.selectList(null);
        //获取[sys_user]的信息
        List<SysUserDTO> userList = sysUserService.getList();
        entityList.stream().forEach(entity -> {
            entity.setCreatorName(sysUserService.getUsernameById(entity.getCreator(), userList));
            entity.setUpdaterName(sysUserService.getUsernameById(entity.getUpdater(), userList));
        });
        return ConvertUtils.sourceToTarget(entityList, PurchaseDetailDTO.class);
    }

    @Override
    public int batchSave(List<PurchaseDetailDTO> dtoList) {
        List<PurchaseDetailEntity> detailList = ConvertUtils.sourceToTarget(dtoList, PurchaseDetailEntity.class);
        return baseDao.batchSave(detailList);
    }

    @Override
    public void save(PurchaseDetailDTO dto) {
        PurchaseDetailEntity entity = ConvertUtils.sourceToTarget(dto, PurchaseDetailEntity.class);
        insert(entity);
    }

}